package com.feicheng.controller.system;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feicheng.common.exception.SystemException;
import com.feicheng.common.result.ResultInfo;
import com.feicheng.common.util.StringGenerator;
import com.feicheng.persistence.entity.Emp;
import com.feicheng.persistence.entity.EmpDept;
import com.feicheng.persistence.entity.FlowRecord;
import com.feicheng.persistence.entity.StepRecord;
import com.feicheng.persistence.entity.User;
import com.feicheng.persistence.model.UserModel;
import com.feicheng.service.impl.FEmpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/admin/emp")
public class EmpController {

	@Autowired
	FEmpServiceImpl empServiceImpl;
	
	@GetMapping("/info")
    public ResultInfo getEmp(Integer empId) {
		if(null == empId) {
			throw new SystemException("empId为空");
		}
    	JSONObject param = new JSONObject();
    	param.put("empId", empId);
    	return empServiceImpl.getEmp(param);
    }
	
	@GetMapping("/all")
	public ResultInfo selectEmpByAll(String deptId, String jobId, String name, String account) {
		JSONObject param = new JSONObject();
		param.put("deptId", deptId);
		param.put("jobId", jobId);
		param.put("name", name);
		param.put("account", account);
		return empServiceImpl.selectEmpByAll(param);
	}

	@PostMapping("/info")
	public ResultInfo newEmp(String user, String emp, String empDept) {
		User userObject = JSONObject.parseObject(user, User.class);
		Emp empObject = JSONObject.parseObject(emp, Emp.class);
		EmpDept empDeptObject = JSONObject.parseObject(empDept, EmpDept.class);

		JSONObject param = new JSONObject();
		param.put("user", userObject);
		param.put("emp", empObject);
		param.put("empDept", empDeptObject);
		return empServiceImpl.newEmp(param);
	}

	@GetMapping("/page")
	public ResultInfo getEmpPage(
			@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size,
			Integer deptId, Integer jobId, String name, String account
	) {
		JSONObject param = new JSONObject();  
		param.put("deptId", deptId);
		param.put("jobId", jobId);
		param.put("name", name);
		param.put("account", account);
		return empServiceImpl.getEmpByPage(current, size, param);
	}

	

	@GetMapping("/dept/page")
	public ResultInfo getDeptOfEmpByPage(Integer empId,  
    		@RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
		JSONObject param = new JSONObject();
		param.put("empId", empId);
		return empServiceImpl.getDeptOfEmpByPage(current, size, param);
	}

	
	//新增请假
	@PostMapping("/flow/record/info") 
	public ResultInfo getFlowReocrd(String flowRecord,String stepRecord) {
		 JSONObject param = new JSONObject();
		param.put("flowRecord", flowRecord);
		param.put("stepRecord",stepRecord);
		return empServiceImpl.getFlowReocrd(param);
	}

	
	@GetMapping("/flow/record/page")
    public ResultInfo getFlowRecordByPage(
    		Integer empId,  
    		@RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
    	JSONObject param = new JSONObject();
        param.put("empId", empId);
    	
    	return empServiceImpl.getFlowRecordByPage(current, size,param);	
    }
    
    @GetMapping("/flow/record/info")
    public ResultInfo getFlowReocrd(Integer flRId) {
    	return empServiceImpl.getFlowReocrd(flRId);
    }
    
    //修改员工信息
    @PutMapping("/info")
    public ResultInfo updateEmp(String user, String emp, String empDept) {

        UserModel userObject = JSONObject.parseObject(user,UserModel.class);
        Emp empObject = JSONObject.parseObject(emp, Emp.class);
        EmpDept empDeptObject = JSONObject.parseObject(empDept, EmpDept.class);

        JSONObject param = new JSONObject();
        param.put("user", userObject);
        param.put("emp", empObject);
        param.put("empDept", empDeptObject);

        return empServiceImpl.updateEmp(param);
    }
    
    //删除员工
    @DeleteMapping("/info")
    public ResultInfo delEmp(Integer empId) {
    	
    	if(empId == null) {
    		throw new SystemException("0");
    	}

        return empServiceImpl.deleteEmp(empId);
    }


    
}
